package rj.cometome;

import java.io.InputStream;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.GLUtils;
import android.util.Log;
import android.widget.TextView;

public class Utils {
	private static Context context = null;
	private static TextView txtView = null;
	private static DebugView dbgView = null;
	
    public static float toRadians(float deg) {
    	return deg * (float)(Math.PI / 180);
    }
    
    public static float toDegrees(float rads) {
    	return rads * (float)(180 / Math.PI);
    }
    
	public static void setDebugView(DebugView dV) {
		dbgView = dV;
	}
	
	public static DebugView getDebugView() {
		return dbgView;
	}
	
	public static void setDebugTextView(TextView txtV) {
		txtView = txtV;
	}
	
	public static void setDebugText(String txt) {
		if (txtView == null) return;
		txtView.setText(txt);
	}
	
	public static void setContext(Context ctxt) {
		if (context != null) return;
		
		context = ctxt;
	}
	
	public static Context getContext() {
		return context;
	}
	
	public static int LoadShader(String strSource, int iType) {
		int[] compiled = new int[1];
		int iShader = GLES20.glCreateShader(iType);
		
		GLES20.glShaderSource(iShader, strSource);
		GLES20.glCompileShader(iShader);
		GLES20.glGetShaderiv(iShader, GLES20.GL_COMPILE_STATUS, compiled, 0);
		
		if (compiled[0] == 0) {
			Log.d("Load Shader Failed", "Compilation\n" + GLES20.glGetShaderInfoLog(iShader));
			return 0;
		}
		
		return iShader;
	}
	
	public static int LoadProgram(String vShaderSource, String fShaderSource) {
		int iVShader, iFShader, iProgID;
		int[] link = new int[1];
		
		iVShader = LoadShader(vShaderSource, GLES20.GL_VERTEX_SHADER);
		
		if (iVShader == 0) {
			Log.d("Load Program", "Vertex Shader Failed");
			return 0;
		}
		
		iFShader = LoadShader(fShaderSource, GLES20.GL_FRAGMENT_SHADER);
		
		if (iFShader == 0) {
			Log.d("Load Program", "Fragment Shader Failed");
			return 0;
		}
		
		iProgID = GLES20.glCreateProgram();
		
		GLES20.glAttachShader(iProgID, iVShader);
		GLES20.glAttachShader(iProgID, iFShader);
		GLES20.glLinkProgram(iProgID);
		GLES20.glGetProgramiv(iProgID, GLES20.GL_LINK_STATUS, link, 0);
		
		if (link[0] <= 0) {
			Log.d("Load Program", "Linking Failed");
			return 0;
		}
		
		GLES20.glDeleteShader(iVShader);
		GLES20.glDeleteShader(iFShader);
		
		return iProgID;
	}
	
	public static int LoadTexture(GLSurfaceView view, int texID) {
		Bitmap img = null;
		int textures[] = new int[1];
		
		try {
			InputStream is = view.getContext().getResources().openRawResource(R.drawable.arrowtex);
			img = BitmapFactory.decodeStream(is);
			
			GLES20.glGenTextures(1, textures, 0);
			GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
			GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
			GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
			GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, img, 0);
		}
		catch(Exception e) {
			Log.d("LoadTexture", e.toString() + ":" + e.getMessage() + ":" + e.getLocalizedMessage());
		}
		img.recycle();
		return textures[0];
	}
}
